package com.demo;


import com.demo.domain.Student;
import com.demo.utils.DbcpUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

/**
 * Hello world!
 *
 */
public class App {
    public static void main( String[] args ) {
        Connection connection = DbcpUtil.getConnection();
//        Statement ps = null;
//        ResultSet rs = null;
//        try {
//            ps = connection.createStatement();
//            rs = ps.executeQuery("select * from student");
//            while (rs.next()) {
//                System.out.print("id: " + rs.getInt("id")
//                    + " name: " + rs.getString("name")
//                    + " age: " + rs.getInt("age") + "\n");
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        } finally {
//            try {
//                if (ps != null) {
//                    if (rs != null)
//                        rs.close();
//                    ps.close();
//                }
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
        QueryRunner queryRunner = new QueryRunner();
        try {
            List<Student> list = queryRunner.query(connection, "select * from student", new BeanListHandler<Student>(Student.class));
            System.out.println(list.size());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
